﻿using System;
using System.Collections.Generic;

namespace Letter.Net.Kcp;

sealed class SafeHashSet<T>
{
    private HashSet<T> hashSet = new HashSet<T>();
    private object sync = new object();

    public int Count
    {
        get
        {
            lock (sync)
            {
                return this.hashSet.Count;
            }
        }
    }

    public void Add(T item)
    {
        lock (sync)
        {
            this.hashSet.Add(item);    
        }
    }

    public void Foreach(Action<T> action)
    {
        if (action == null)
        {
            return;
        }

        foreach (var item in this.hashSet)
        {
            action(item);
        }
    }

    public bool Contains(T item)
    {
        lock (sync)
        {
            return this.hashSet.Contains(item);    
        }
    }

    public bool Remove(T item)
    {
        lock (sync)
        {
            return this.hashSet.Remove(item);    
        }
    }

    public void Clear()
    {
        lock (sync)
        {
            this.hashSet.Clear();    
        }
    }
}